package com.example.medicinetime;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DBManager {
    public static final String TAG = "DBManager";
    private DBHelper dbHelper;
    private String TBNAME;
    private String TBNAME2;

    public DBManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
        TBNAME2 = DBHelper.TB_NAME2;
    }

    public void add(MedicienRecord item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("mname", item.getMedicienName());
        values.put("starttime", item.getStartTime());
        values.put("duration", item.getDuration());
        values.put("daytime", item.getDailytime());
        values.put("dosis", item.getDosis());
        values.put("alarmid",item.getAlarmId());
        db.insert(TBNAME, null, values);
        db.close();
    }

    public void add_completed(FinishRecord item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("mname", item.getF_medicien_name());
        values.put("finishdate", item.getFinishDate());
        values.put("finishtime", item.getFinishtime());
        values.put("finishdosis", item.getF_dosis());
        db.insert(TBNAME2, null, values);
        db.close();
    }


    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }


    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Log.i(TAG, "delete: 删除前");
        db.delete(TBNAME, "ID=?", new String[]{String.valueOf(id)});
        Log.i(TAG, "delete: 删除"+"ID=?"+id);
        db.close();
    }

    public void update(MedicienRecord item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("mname", item.getMedicienName());
        values.put("starttime", item.getStartTime());
        values.put("duration", item.getDuration());
        values.put("daytime", item.getDailytime());
        values.put("dosis", item.getDosis());
        values.put("alarmid",item.getAlarmId());
        db.update(TBNAME, values, "ID=?", new String[]{String.valueOf(item.getId())});
        db.close();
    }


    @SuppressLint("Range")
    public List<MedicienRecord> listAll(){
        List<MedicienRecord> recordList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            recordList = new ArrayList<MedicienRecord>();
            while(cursor.moveToNext()){
                MedicienRecord item = new MedicienRecord();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setMedicienName(cursor.getString(cursor.getColumnIndex("MNAME")));
                item.setStartTime(cursor.getString(cursor.getColumnIndex("STARTTIME")));
                item.setDuration(cursor.getInt(cursor.getColumnIndex("DURATION")));
                item.setDailytime(cursor.getString(cursor.getColumnIndex("DAYTIME")));
                item.setDosis(cursor.getInt(cursor.getColumnIndex("DOSIS")));
                item.setAlarmId(cursor.getInt(cursor.getColumnIndex("ALARMID")));
                Log.i(TAG, "listAll: item = "+cursor.getInt(cursor.getColumnIndex("ID"))+"\t"+cursor.getString(cursor.getColumnIndex("MNAME"))+"AlarmId="+cursor.getString(cursor.getColumnIndex("ALARMID")));
                recordList.add(item);
            }
            cursor.close();
        }
        db.close();
        return recordList;

    }

    @SuppressLint("Range")
    public List<FinishRecord> listAll_completed(){
        List<FinishRecord> recordList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME2, null, null, null, null, null, null);
        if(cursor!=null){
            recordList = new ArrayList<FinishRecord>();
            while(cursor.moveToNext()){
                FinishRecord item = new FinishRecord();
                item.setId(cursor.getInt(cursor.getColumnIndex("ID")));
                item.setF_medicien_name(cursor.getString(cursor.getColumnIndex("MNAME")));
                item.setFinishDate(cursor.getString(cursor.getColumnIndex("FINISHDATE")));
                item.setF_dosis(cursor.getInt(cursor.getColumnIndex("FINISHDOSIS")));
                item.setFinishtime(cursor.getString(cursor.getColumnIndex("FINISHTIME")));
                Log.i(TAG, "listAll_completed: ID="+cursor.getInt(cursor.getColumnIndex("ID"))+"\tMNAME="+cursor.getString(cursor.getColumnIndex("MNAME")));
                recordList.add(item);
            }
            cursor.close();
        }
        db.close();
        return recordList;

    }


}
